from rest_framework import mixins
from rest_framework import views
from rest_framework import viewsets
from rest_framework import permissions
from rest_framework import filters
from django_filters.rest_framework import DjangoFilterBackend
from rest_framework import status
from rest_framework.response import Response
from rest_framework.authentication import SessionAuthentication
from rest_framework_jwt.authentication import JSONWebTokenAuthentication
from .models import redisInfo
from .serializers import RedisDetailSerializer
from .serializers import RedisCreateSerializer
from libs.paginations import ListPagination
from .utils.redis_opration import RedisOpration


class DatabaseViewSet(viewsets.GenericViewSet,
                      mixins.ListModelMixin,
                      mixins.DestroyModelMixin,
                      mixins.CreateModelMixin,
                      mixins.UpdateModelMixin,
                      mixins.RetrieveModelMixin):
    '''
    获取缓存信息
    '''

    queryset = redisInfo.objects.all().order_by('id')
    permission_classes = (permissions.IsAuthenticated,)
    authentication_classes = (JSONWebTokenAuthentication,SessionAuthentication)
    pagination_class = ListPagination
    filter_backends = (filters.SearchFilter, filters.OrderingFilter)
    search_fields = ("name",)

    def get_serializer_class(self):
        if self.action == 'create' or self.action == 'update':
            return RedisCreateSerializer
        else:return RedisDetailSerializer

    def retrieve(self, request, *args, **kwargs):
        instance = self.get_object()
        serializer = self.get_serializer(instance)
        return Response(serializer.data)


class RedisOprationView(views.APIView):
    '''
    redis连通状态及缓存清理
    '''
    permission_classes = (permissions.IsAuthenticated,)
    authentication_classes = (JSONWebTokenAuthentication,SessionAuthentication)
    def get(self,request,*args,**kwargs):
        '''测试redis连通性  传参：{'id':int}'''

        instance = redisInfo.objects.filter(id=self.request.query_params['id']).values().first()
        connect = RedisOpration(host=instance['address'], port=instance['port'], password=instance['password'])
        return Response(connect.TestRedisConnect(),status=status.HTTP_200_OK)

    def delete(self,request,*args,**kwargs):
        '''清理缓存  传参：{'id':int,'dbNumber':int}'''

        instance = redisInfo.objects.filter(id=request.data['id']).values().first()
        if 'dbNumber' not in request.data:
            dbNumber = None
        else:dbNumber = request.data['dbNumber']
        connect = RedisOpration(host=instance['address'],port=instance['port'],password=instance['password'],db=dbNumber)
        return Response(connect.CleanRedisDB(),status=status.HTTP_200_OK)


from abc import abstractmethod


class Fly(object):
    @abstractmethod
    def fly(self, x):
        pass


class Duck(Fly):
    def fly(self, x):
        print(f"duck can fly {x} M")


class Ji(Fly):
    pass


class Tiger(object):
    def walk(self):
        pass